package com.cac.demo.newCode.sorting.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cac.demo.newCode.materialStorage.entity.WmsDeliveryPlan;
import com.cac.demo.newCode.materialStorage.service.IWmsDeliveryPlanService;
import com.cac.demo.newCode.sorting.entity.*;
import com.cac.demo.newCode.sorting.mapper.MsConsoleMapper;
import com.cac.demo.newCode.sorting.mapper.SortingTaskMapper;
import com.cac.demo.newCode.sorting.service.*;
import com.cac.demo.newCode.sorting.utils.IPUtils;
import com.cac.demo.newCode.sorting.vo.MaterialSortedVO;
import com.cac.demo.newCode.sorting.vo.SortingTaskVO;
import com.cac.demo.newCode.sorting.vo.WorkBinMaterialVO;
import com.cac.demo.newCode.sorting.vo.WorkBinVO;
import com.cac.demo.util.CreateCommonLog;
import com.cac.demo.util.adapter.ComUtil;
import com.cac.demo.util.adapter.ResponseMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.cac.demo.newCode.sorting.dto.SortingTaskDTO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SortingTaskServiceImpl implements SortingTaskService {

    @Resource
    SortingTaskMapper sortingTaskMapper;
    @Resource
    IMsGkLogService msGkLogService;
    @Resource
    IMsAssginWorkbintoolRelService msAssginWorkbintoolRelService;
    @Resource
    CreateCommonLog createCommonLog;
    @Resource
    IMsTaskPackageService iMsTaskPackageService;
    @Resource
    IMsTaskAoService iMsTaskAoService;
    @Resource
    IMsAoMatchBillService iMsAoMatchBillService;
    @Resource
    IWmsDeliveryPlanService iWmsDeliveryPlanService;
    @Resource
    IMsPackageMaterialService iMsPackageMaterialService;
    @Resource
    MsConsoleMapper msConsoleMapper;

    /**
     * 开始作业
     *
     * @param sortingTaskDTOs
     * @return
     */
    @Transactional
    @Override
    public ResponseMsg<Object> startSortingTask(List<SortingTaskDTO> sortingTaskDTOs) {
        String IP = IPUtils.getIP();
        for (int i = 0; i < sortingTaskDTOs.size(); i++) {
//            if (sortingTaskDTOs.get(i).getConsoleId() != null && sortingTaskDTOs.get(i).getConsoleId() != "") {
//                LambdaQueryWrapper<MsConsole> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(MsConsole::getConsoleId, sortingTaskDTOs.get(i).getConsoleId());
//                MsConsole msConsole = msConsoleMapper.selectOne(queryWrapper);
//                String selectIP = msConsole.getConsoleIp();
//                if(!selectIP.equals(IP)){
//                    return new ResponseMsg<>().setRetCode("201").setMessage("当前扫描的分拣台和系统配置的分拣台不一致，请检查分拣台状态");
//                }
//            }
            String workbinCode = sortingTaskDTOs.get(i).getWorkbinCode();
            String toolCode = sortingTaskDTOs.get(i).getToolCode();
            String packageCode = sortingTaskDTOs.get(i).getPackageCode();
            //更新此托盘状态
            LambdaUpdateWrapper<MsAssginWorkbintoolRel> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(MsAssginWorkbintoolRel::getIsCreateTask, 'Y');
            updateWrapper.set(MsAssginWorkbintoolRel::getAttribute1, "1");
            updateWrapper.eq(MsAssginWorkbintoolRel::getToolCode, toolCode);
            updateWrapper.eq(MsAssginWorkbintoolRel::getWorkbinCode, workbinCode);
            updateWrapper.eq(MsAssginWorkbintoolRel::getDataStatus, '1');
            updateWrapper.eq(MsAssginWorkbintoolRel::getIsSort, 'N');
            if (i == 0) {
                //创建托盘任务
                int i1 = sortingTaskMapper.insertToolGkLog(IP, toolCode, packageCode);
                if (i1 > 0) {
                    msAssginWorkbintoolRelService.update(updateWrapper);
                }
            } else {
                //获取前一个
                String temp = sortingTaskDTOs.get(i - 1).getToolCode();
                //判断是否相同 相同则不重新创建
                if (!(temp.equals(toolCode))) {
                    //创建托盘任务
                    int i1 = sortingTaskMapper.insertToolGkLog(IP, toolCode, packageCode);
                    if (i1 >= 0) {
                        msAssginWorkbintoolRelService.update(updateWrapper);
                    }
                }
            }


            if (Objects.isNull(workbinCode)) {
                continue;
            }
            if ("312080030000".equals(workbinCode)) {
                //创建大件任务
                sortingTaskMapper.insertBigWorkbinGkLog(IP, packageCode);
            } else {
                //查询当前任务是否创建
                LambdaQueryWrapper<MsGkLog> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MsGkLog::getWorkbinCode, workbinCode);
                queryWrapper.eq(MsGkLog::getIsBack, 'N');
                queryWrapper.eq(MsGkLog::getIsFinish, 'N');
                if (msGkLogService.count(queryWrapper) > 0) {
                    sortingTaskMapper.insertSuppleGkLog(IP, workbinCode);
                } else {
                    //创建箱子任务
                    sortingTaskMapper.insertWorkbinGkLog(IP, workbinCode, packageCode);
                }
            }
            //下发任务
            HashMap gkTask = new HashMap();
            gkTask.put("func", "sort");
            //下发缓存箱出库指令
            gkTask.put("boxType", "WORKBIN");
            //结束位置
            gkTask.put("endCode", "xyssj" + sortingTaskMapper.findStartCode(IP, (String) gkTask.get("boxType")));
            //首次下发命令标识符
            gkTask.put("firstCall", "firstCall");
            StringBuffer sb = new StringBuffer();
            //调用封装下发任务
            HashMap wMap = createCommonLog.callWCSInterface(gkTask);
            sb.append("workbin===").append(wMap.get("msg"));
            //下发工作包出库指令
            gkTask.replace("boxType", "TOOL");
            //结束位置
            gkTask.put("endCode", "xyssj" + sortingTaskMapper.findStartCode(IP, (String) gkTask.get("boxType")));
            //调用封装下发任务
            HashMap tMap = createCommonLog.callWCSInterface(gkTask);
            sb.append(",tool===").append(tMap.get("msg"));
        }
        return new ResponseMsg().setRetCode("200").setMessage("操作成功");
    }

    /**
     * AO分拣任务列表
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> AOSortingTaskPage(SortingTaskDTO sortingTaskDTO) {
        Page<?> page = new Page<>(sortingTaskDTO.getCurrent(), sortingTaskDTO.getSize());
        String ip = IPUtils.getIP();
        sortingTaskDTO.setIP(ip);
        IPage<MaterialSortedVO> sortingTaskVOIPage = sortingTaskMapper.materialSorted(page, sortingTaskDTO);
        return new ResponseMsg<>().setResponseBody(sortingTaskVOIPage);
    }

    /**
     * 清理等待任务
     *
     * @return
     */
    @Transactional
    @Override
    public ResponseMsg<Object> cleanWaitingTask() {
        String IP = IPUtils.getIP();
        SortingTaskDTO dto = sortingTaskMapper.searchLocation(IP);
        int result = 0;
        if (Objects.isNull(dto)) {
            return new ResponseMsg<>().setRetCode("201").setMessage("操作台信息有误");
        } else {
            result += sortingTaskMapper.delGkLog(dto);
            result += sortingTaskMapper.deleteBigLog(dto);
            result += sortingTaskMapper.delGkLogSupple(dto);
        }
        if (result <= 0) {
            return new ResponseMsg<>().setMessage("当前操作台任务无可清理任务").setRetCode("200");
        } else {
            return new ResponseMsg<>().setMessage("清理成功").setRetCode("200");
        }
    }

    /**
     * 托盘查询
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> getToolCode(SortingTaskDTO sortingTaskDTO) {
        List<SortingTaskVO> sortingTaskVOS = sortingTaskMapper.queryReplaceTool(sortingTaskDTO);
        return new ResponseMsg<>().setResponseBody(sortingTaskVOS).setRetCode("200");
    }

    /**
     * 查看缓存箱
     *
     * @return
     */
    @Override
    public ResponseMsg<Object> viewWorkBin() {
        String IP = IPUtils.getIP();
        List<WorkBinVO> workBinVOS = sortingTaskMapper.viewWorkBin(IP);
        return new ResponseMsg<>().setResponseBody(workBinVOS);
    }

    /**
     * 缓存箱扫码
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> scanConfirmMaterialList(SortingTaskDTO sortingTaskDTO) {
        List<MaterialSortedVO> materialSortedVOList = sortingTaskMapper.scanConfirmMaterial(sortingTaskDTO);
        return new ResponseMsg<>().setResponseBody(materialSortedVOList);

    }

    /**
     * 扫码确认的物料分页
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> scanConfirmMaterialPage(SortingTaskDTO sortingTaskDTO) {
        Page<?> page = new Page<>(sortingTaskDTO.getCurrent(), sortingTaskDTO.getSize());
        IPage<MaterialSortedVO> materialSortedVOIPage = sortingTaskMapper.scanConfirmMaterialPage(page, sortingTaskDTO);
        return new ResponseMsg<>().setResponseBody(materialSortedVOIPage);
    }

    /**
     * 缓存箱任务下发
     *
     * @return
     */
    @Override
    public ResponseMsg<Object> materialDistribution() {
        String IP = IPUtils.getIP();
        String operationBit = null;
        HashMap workbin = new HashMap();
        //获取操作编码
        operationBit = sortingTaskMapper.findStartCode(IP, "WORKBIN");
        workbin.put("endCode", "xyssj" + operationBit);
        workbin.put("func", "sort");
        workbin.put("boxType", "WORKBIN");
        //调用流量控制->缓存箱
        HashMap callRtrun = createCommonLog.callWCSInterface(workbin);
        if ("201".equals(callRtrun.get("retCode").toString())) {
            return new ResponseMsg<>().setRetCode((String) callRtrun.get("retCode")).setMessage((String) callRtrun.get("msg"));
        }
        //调用流量控制->托盘
        HashMap tool = new HashMap();
        operationBit = sortingTaskMapper.findStartCode(IP, "TOOL");
        tool.put("endCode", "xyssj" + operationBit);
        tool.put("func", "sort");
        tool.put("boxType", "TOOL");
        HashMap rsl = createCommonLog.callWCSInterface(tool);
        if ("201".equals(rsl.get("retCode").toString())) {
            return new ResponseMsg<>().setRetCode((String) rsl.get("retCode")).setMessage((String) rsl.get("msg"));
        }
        return new ResponseMsg<>().setRetCode((String) callRtrun.get("retCode")).setMessage((String) callRtrun.get("msg"));
    }

    /**
     * 绑定托盘箱子
     *
     * @return
     */
    @Transactional
    public ResponseMsg<Object> bindingWorkBin() {
        try {
            //清理没有创建任务的托盘箱子
            LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MsAssginWorkbintoolRel::getIsSort, 'N');
            msAssginWorkbintoolRelService.remove(queryWrapper);
            //写入未分拣的箱子到关系表中，准备和托盘绑定关系
            sortingTaskMapper.saveAssignWorkbinToolRel();
            //绑定箱子对应的托盘
            queryWrapper.eq(MsAssginWorkbintoolRel::getIsCreateTask, 'N');
            queryWrapper.eq(MsAssginWorkbintoolRel::getDataStatus, '1');
            List<MsAssginWorkbintoolRel> list = msAssginWorkbintoolRelService.list(queryWrapper);
            for (MsAssginWorkbintoolRel msAssginWorkbintoolRel : list) {
                String toolCode = sortingTaskMapper.getToolCodeByPackage(msAssginWorkbintoolRel);
                if (!Objects.isNull(toolCode)) {
                    msAssginWorkbintoolRel.setToolCode(toolCode);
                } else {
                    toolCode = sortingTaskMapper.getToolCodeByRule(msAssginWorkbintoolRel);
                    msAssginWorkbintoolRel.setToolCode(toolCode);
                    msAssginWorkbintoolRelService.updateById(msAssginWorkbintoolRel);
                }
            }
            return new ResponseMsg<>().setMessage("分配成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new ResponseMsg<>().setMessage("分配失败").setRetCode("201");
        }
    }

    /**
     * 叫箱
     *
     * @param sortingTaskDTO
     * @return
     */
    @Transactional
    @Override
    public ResponseMsg<Object> callWorkBin(SortingTaskDTO sortingTaskDTO) {
        String IP = IPUtils.getIP();
        String msAoMatchInfoId = ComUtil.getId();
        sortingTaskDTO.setMsAoMatchInfoId(msAoMatchInfoId);
        //插入一条新的配套数据 ms_ao_match_info
        sortingTaskMapper.saveNewAoMatchInfo(sortingTaskDTO);
        //插入一条 ms_assgin_workbintool_rel
        sortingTaskMapper.saveAssignWorkbinToolRelByMatchInfoId(sortingTaskDTO);
        //更新 原来ms_ao_match_info表数据
        sortingTaskMapper.updatePreMatchInfo(sortingTaskDTO);
        //更新原来的ms_assgin_workbintool_rel数据状态
        sortingTaskMapper.updateAssginWorkbinToolRelStatus(sortingTaskDTO);
        //创建日志任务
//        //查询当前任务是否创建
        LambdaQueryWrapper<MsGkLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsGkLog::getWorkbinCode, sortingTaskDTO.getWorkbinCode());
        queryWrapper.eq(MsGkLog::getIsBack, 'N');
        queryWrapper.eq(MsGkLog::getIsFinish, 'N');
        if (msGkLogService.count(queryWrapper) > 0) {
            sortingTaskMapper.insertSuppleGkLog(IP, sortingTaskDTO.getWorkbinCode());
        } else {
            //创建箱子任务
            sortingTaskMapper.insertWorkbinGkLog(IP, sortingTaskDTO.getWorkbinCode(), sortingTaskDTO.getPackageCode());
        }
        //给wcs下达指令
        //下发任务
        HashMap gkTask = new HashMap();
        gkTask.put("func", "sort");
        //下发缓存箱出库指令
        gkTask.put("boxType", "WORKBIN");
        //结束位置
        gkTask.put("endCode", "xyssj" + sortingTaskMapper.findStartCode(IP, (String) gkTask.get("boxType")));
        //首次下发命令标识符
        gkTask.put("firstCall", "firstCall");
        StringBuffer sb = new StringBuffer();
        //调用封装下发任务
        HashMap wMap = createCommonLog.callWCSInterface(gkTask);
        sb.append("workbin===").append(wMap.get("msg"));
        return new ResponseMsg().setRetCode("200").setMessage("操作成功").setErrorDesc(sb.toString());
    }

    /**
     * 相同图纸的物料
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> SameDrawingMaterial(SortingTaskDTO sortingTaskDTO) {
        List<WorkBinMaterialVO> workBinMaterialVOS = sortingTaskMapper.SameDrawingMaterial(sortingTaskDTO);
        return new ResponseMsg<>().setResponseBody(workBinMaterialVOS);
    }

    /**
     * 更换托盘
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> replaceToolCode(SortingTaskDTO sortingTaskDTO) {
        LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper = new LambdaQueryWrapper<>();
        if (sortingTaskDTO.getMsAssginWorkbintoolRelId() != null && sortingTaskDTO.getMsAssginWorkbintoolRelId() != "") {
            queryWrapper.eq(MsAssginWorkbintoolRel::getMsAssginWorkbintoolRelId, sortingTaskDTO.getMsAssginWorkbintoolRelId());
        }
        queryWrapper.eq(MsAssginWorkbintoolRel::getToolCode, sortingTaskDTO.getSourceToolCode());
        queryWrapper.eq(MsAssginWorkbintoolRel::getIsCreateTask, 'N');
        queryWrapper.eq(MsAssginWorkbintoolRel::getIsSort, 'N');
        List<MsAssginWorkbintoolRel> list = msAssginWorkbintoolRelService.list(queryWrapper);
        for (MsAssginWorkbintoolRel msAssginWorkbintoolRel : list) {
            msAssginWorkbintoolRel.setToolCode(sortingTaskDTO.getTargetToolCode());
            LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MsAssginWorkbintoolRel::getMsAssginWorkbintoolRelId, msAssginWorkbintoolRel.getMsAssginWorkbintoolRelId());
            msAssginWorkbintoolRelService.update(msAssginWorkbintoolRel, queryWrapper1);
        }
        return new ResponseMsg().setRetCode("200").setMessage("操作成功");
    }

    /**
     * 保存备注
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg SortingSaveRemark(SortingTaskDTO sortingTaskDTO) {
        try {
            LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MsAssginWorkbintoolRel::getMsAssginWorkbintoolRelId, sortingTaskDTO.getMsAssginWorkbintoolRelId());
            MsAssginWorkbintoolRel msAssginWorkbintoolRel = msAssginWorkbintoolRelService.getOne(queryWrapper);
            WmsDeliveryPlan wmsDeliveryPlan = iWmsDeliveryPlanService.getById(msAssginWorkbintoolRel.getWmsDeliveryPlanId());
            wmsDeliveryPlan.setReceiveRemark(sortingTaskDTO.getReceiveRemark());
            iWmsDeliveryPlanService.updateById(wmsDeliveryPlan);
            return new ResponseMsg().setRetCode("200").setMessage("操作成功");
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }

    }

    /**
     * 手动更换托盘
     *
     * @param sortingTaskDTO
     * @return
     */
    @Transactional
    @Override
    public ResponseMsg<Object> replaceToolCodeByHand(SortingTaskDTO sortingTaskDTO) {
        LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper = new LambdaQueryWrapper<>();
        if (sortingTaskDTO.getMsAssginWorkbintoolRelId() != null && sortingTaskDTO.getMsAssginWorkbintoolRelId() != "") {
            queryWrapper.eq(MsAssginWorkbintoolRel::getMsAssginWorkbintoolRelId, sortingTaskDTO.getMsAssginWorkbintoolRelId());
        }
        queryWrapper.eq(MsAssginWorkbintoolRel::getIsSort, 'N');
        queryWrapper.eq(MsAssginWorkbintoolRel::getMsTaskAoId, sortingTaskDTO.getMsTaskAoId());
        List<MsAssginWorkbintoolRel> list = msAssginWorkbintoolRelService.list(queryWrapper);
        for (MsAssginWorkbintoolRel msAssginWorkbintoolRel : list) {
            msAssginWorkbintoolRel.setToolCode(sortingTaskDTO.getToolCode());
            LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MsAssginWorkbintoolRel::getMsAssginWorkbintoolRelId, msAssginWorkbintoolRel.getMsAssginWorkbintoolRelId());
            msAssginWorkbintoolRelService.update(msAssginWorkbintoolRel, queryWrapper1);
        }
        return new ResponseMsg().setRetCode("200").setMessage("操作成功");
    }

    /**
     * 查看工作包规则
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> getPackCode(SortingTaskDTO sortingTaskDTO) {
        LambdaQueryWrapper<MsPackageMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsPackageMaterial::getPackageCode, sortingTaskDTO.getPackageCode());
        queryWrapper.orderByAsc(MsPackageMaterial::getAoCode);
        List<MsPackageMaterial> list = iMsPackageMaterialService.list(queryWrapper);
        return new ResponseMsg<>().setResponseBody(list);
    }

    /**
     * 工作包-查看物料信息
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> getMaterial(SortingTaskDTO sortingTaskDTO) {
        Page<?> page = new Page<>(sortingTaskDTO.getCurrent(), sortingTaskDTO.getSize());
        LambdaQueryWrapper<MsTaskPackage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsTaskPackage::getMsTaskPackageId, sortingTaskDTO.getMsTaskPackageId());
        MsTaskPackage msTaskPackage = iMsTaskPackageService.getOne(queryWrapper);
        sortingTaskDTO.setPackageCode(msTaskPackage.getPackageCode());
        IPage<MsAoMatchInfo> material = sortingTaskMapper.getMaterial(page, sortingTaskDTO);
        return new ResponseMsg<>().setResponseBody(material);
    }

    /**
     * 暂停作业
     *
     * @param sortingTaskDTOs
     * @return
     */
    @Override
    public ResponseMsg<Object> pauseSortingTask(List<SortingTaskDTO> sortingTaskDTOs) {
        for (SortingTaskDTO sortingTaskDTO : sortingTaskDTOs) {
            LambdaUpdateWrapper<MsAssginWorkbintoolRel> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(MsAssginWorkbintoolRel::getAttribute1, "0");
            updateWrapper.eq(MsAssginWorkbintoolRel::getMsAssginWorkbintoolRelId, sortingTaskDTO.getMsAssginWorkbintoolRelId());
            msAssginWorkbintoolRelService.update(updateWrapper);
        }
        cleanWaitingTask();
        return new ResponseMsg<>().setResponseBody("暂停成功");
    }

    /**
     * 查看缓存箱和托盘
     *
     * @return
     */
    @Override
    public ResponseMsg<Object> getWorkBinAndTool(List<SortingTaskDTO> sortingTaskDTOs) {
        HashMap<Object, Object> result = new HashMap<>();
        //所有工作包ID
        List<String> packageIds = sortingTaskDTOs.stream()
                .map(SortingTaskDTO::getMsTaskPackageId)
                .collect(Collectors.toList());
        //去重
        packageIds = packageIds.stream().distinct().collect(Collectors.toList());
        //托盘队列
        List<String> toolCodes = sortingTaskDTOs.stream()
                .map(SortingTaskDTO::getToolCode)
                .collect(Collectors.toList());
        //去重
        toolCodes = toolCodes.stream().distinct().collect(Collectors.toList());
        //缓存箱队列
        List<String> workbinCodes = sortingTaskDTOs.stream()
                .map(SortingTaskDTO::getWorkbinCode)
                .collect(Collectors.toList());
        //去重
        workbinCodes = workbinCodes.stream().distinct().collect(Collectors.toList());
        //托盘位置
        String ToolEndCode = msConsoleMapper.getConsoleIp(IPUtils.getIP());
        if(StringUtils.isBlank(ToolEndCode)){
            return new ResponseMsg<>().setRetCode("201").setMessage("获取分拣台失败,请查看分拣台状态");
        }
        //缓存箱位置
        String workBinEndCode = msConsoleMapper.getworkBinEndCode(IPUtils.getIP());
        if(StringUtils.isBlank(workBinEndCode)){
            return new ResponseMsg<>().setRetCode("201").setMessage("获取分拣台失败,请查看分拣台状态");
        }
        //当前托盘
        List<SortingTaskDTO> currentTool = sortingTaskMapper.getCurrentTool(ToolEndCode,toolCodes,packageIds);
        //当前箱子
        List<SortingTaskDTO> currentWorkBin = sortingTaskMapper.getCurrentWorkBin(workBinEndCode,workbinCodes,packageIds);
        //等待箱子
        List<SortingTaskDTO> waitWorkBin = sortingTaskMapper.getWaitWorkBin(workBinEndCode,workbinCodes,packageIds);
        //等待托盘
        List<SortingTaskDTO> waitTool = sortingTaskMapper.getWaitTool(ToolEndCode,toolCodes,packageIds);
        result.put("toolQueue", toolCodes);
        result.put("workBinQueue", workbinCodes);
        result.put("currentWorkBin", currentWorkBin);
        result.put("currentTool", currentTool);
        result.put("waitWorkBin", waitWorkBin);
        result.put("waitTool", waitTool);
        return new ResponseMsg<>().setResponseBody(result);
    }

    /**
     * 分拣暂停 用于流量控制
     *
     * @return
     */
    public boolean getPauseTask() {
        String ip = IPUtils.getIP();
        List<SortingTaskDTO> tray = sortingTaskMapper.getTray(ip);
        List<String> collect = tray.stream().map(SortingTaskDTO::getToolCode).collect(Collectors.toList());
        if (collect.size() != 0) {
            LambdaQueryWrapper<MsAssginWorkbintoolRel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MsAssginWorkbintoolRel::getIsCreateTask, 'Y');
            queryWrapper.in(MsAssginWorkbintoolRel::getToolCode, collect);
            queryWrapper.eq(MsAssginWorkbintoolRel::getDataStatus, '1');
            queryWrapper.eq(MsAssginWorkbintoolRel::getIsSort, 'N');
            List<MsAssginWorkbintoolRel> list = msAssginWorkbintoolRelService.list(queryWrapper);
            List<MsAssginWorkbintoolRel> collect1 = list.stream()
                    .filter(m -> m.getAttribute1().equals("0"))
                    .collect(Collectors.toList());
            if (collect1 != null && collect1.size() > 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * PDA扫描分拣台
     *
     * @param sortingTaskDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> getConsole(SortingTaskDTO sortingTaskDTO) {
        String consoleId = sortingTaskDTO.getConsoleId();
        LambdaQueryWrapper<MsConsole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsConsole::getConsoleId, consoleId);
        MsConsole msConsole = msConsoleMapper.selectOne(queryWrapper);
        return new ResponseMsg<>().setResponseBody(msConsole);

    }

}
